package com.activeMQ.student.topic;

import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;

/**
 * 主题的发送者
 * @date 2020/10/7 15:18
 */
public class TestProducer {

    private static String brokerURL = "tcp://106.12.51.117:61617";
    private static String topicName = "topic-Hello";

    public static void main(String[] args) throws JMSException {

        ConnectionFactory factory = new ActiveMQConnectionFactory(brokerURL);

        Connection connection = factory.createConnection();

        connection.start();

        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

        Topic topic = session.createTopic(topicName);

        MessageProducer producer = session.createProducer(topic);

        for (int i = 0; i <= 10 ; i++) {

            TextMessage textMessage = session.createTextMessage("topic-message:" + i);

            producer.send(textMessage);

        }

        producer.close();
        session.close();
        connection.close();
        System.out.println("主题消息发送完成");
    }
}
